
export function pageInfo(pageInfo) {
	let temp = {};

	temp.firstPage = pageInfo.firstPage;
	temp.lastPage = pageInfo.lastPage;

	temp.pageNumber = pageInfo.pageNumber;
	temp.pageSize = pageInfo.pageSize;

	temp.totalPage = pageInfo.totalPage;
	temp.totalRow = pageInfo.totalRow;



	const DISABLED = "disabled";

	temp.firstItemClass = pageInfo.firstPage ? DISABLED : '';
	temp.lastItemClass = pageInfo.lastPage ? DISABLED : '';

	return Object.freeze(temp);
}


export function parsePagination(pageNumber, totalPage) {
	const paginations = pagination(pageNumber, totalPage);

	let items = [];

	for (let item of paginations) {
		if (!item) {
			items.push({ class: "disabled" });
			continue;
		}


		let temp = { value: item };

		if (item === pageNumber) {
			temp.class = "active";
		}

		items.push(temp);
	}

	return Object.freeze(items);
	// console.log("items: ", items);
}

/**
 * 分页算法
 * @param  object pageInfo 
 *				  - pageNumber,
 *				  - totalPage,
 * @return array [description]
 */
function pagination(pageNumber, totalPage) {
	const page = pageNumber;
	
	if (totalPage <= 11) {
		return [1, 2, 3 , 4, 5, 6, 7, 8, 9, 10, 11]
				.splice(0, totalPage);
	}



	let size = (page <= 6 || page + 6 >= totalPage) ? 12 : 13;
	let nums = new Array(size);

	const fixedLength = 2;

	// nums = [1, 2, ....]
	for (let i = 0; i < fixedLength; i++) {
		nums[i] = i + 1;
	}

	// nums = [1, 2, ...., totalPage - 1, totalPage]
	for (let i = 0; i < fixedLength; i++) {
		nums[size - i - 1] = totalPage - i;
	}






	let start = 2, end = size - 3;
	nums[start++] = nums[end--] = null;


	if (12 === size) {
		page <= 6 ? start-- : end++;
	}



	let s, e;

	if (start === 2) {
		// console.log("1...")
		s = 3;
		e = 9;
	} else if(end === size - 3) {
		// console.log("2...")
		s = totalPage - 8;
		e = totalPage - 2;
	} else {
		// console.log("3...")
		s = page - 3;
		e = page + 3;
	}



	for (let i = s; i <= e; i++) {
		nums[start++] = i;
	}

	return nums;
}
